package ormtest

import (
	_ "database/sql"
	"fmt"
	"log"
	"time"

	"gorm.io/driver/mysql"
	_ "gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

func init() {
	dsn := "root:baibai123@tcp(localhost:3306)/crm?parseTime=true"
	con, err := gorm.Open(mysql.Open(dsn))
	if err != nil {
		log.Fatal(err)
		return
	}
	db = con
}

// 大写开头才能访问
type Customer struct {
	ID          int
	ALIAS       string
	aGE         int
	BIRTH_DATE  time.Time
	BIRTH_PLACE string
	FIELD1      string
	FIELD2      string
}

func CustomerOps() {
	// var cus = Customer{BIRTH_DATE:time.Now(),ALIAS:"12.2.323.32",aGE:121}
	cusList := BatchNew(10000)
	fmt.Println(len(cusList))
	for i := 30; i < 40; i++ {
		fmt.Println(cusList[i])
	}
	db.Table("customer").CreateInBatches(cusList, len(cusList))
}

func CustomerQuery() {
	// 链式，所以结果保存在参数中
	var cus []Customer
	// 泛型支持不好，没有具体到具体类型
	db.Table("customer").Find(&cus, Customer{ALIAS: "qiang1212121125"})
	fmt.Println(len(cus))
	fmt.Println(cus)

}
